package cn.dylan.user.service.impl;

import cn.dylan.basic.constant.RegisterConstant;
import cn.dylan.basic.exception.BusinessException;
import cn.dylan.basic.service.impl.BaseServiceImpl;
import cn.dylan.basic.util.Md5Utils;
import cn.dylan.basic.util.StrUtils;
import cn.dylan.user.domain.Logininfo;
import cn.dylan.user.domain.User;
import cn.dylan.user.dto.RegisterDto;
import cn.dylan.user.mapper.LogininfoMapper;
import cn.dylan.user.mapper.UserMapper;
import cn.dylan.user.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    public void phoneRegister(RegisterDto dto) {

        String phone = dto.getPhone();
        String verifyCode = dto.getVerifyCode();
        String password = dto.getPassword();
        String comfirmPassword = dto.getComfirmPassword();

        // 1. 校验  非空校验
        if(StringUtils.isEmpty(phone)
                ||StringUtils.isEmpty(verifyCode)
                ||StringUtils.isEmpty(password)
                ||StringUtils.isEmpty(comfirmPassword)){
            throw new BusinessException("参数不能为空！");
        }

        // 2. 验证两次密码是否一致
        if(!password.equals(comfirmPassword)){
            throw new BusinessException("两次密码不一致，请重新输入！");
        }

        // 3. 手机号校验，是否被注册
        User userPhone = userMapper.findByPhone(phone);
        if(userPhone!=null){
            throw new BusinessException("手机已经注册，请直接登录！");
        }

        // 4. 验证码是否过期
        Object obj = redisTemplate.opsForValue().get(phone + RegisterConstant.PHONEREGISTER);
        if(obj==null){
            // 验证码过期了
            throw new BusinessException("验证码已过期，请重新获取");
        }

        // 5. 校验验证码是否正确
        String redisCode = obj.toString().split(":")[0];
        if(!verifyCode.equalsIgnoreCase(redisCode)){
            throw new BusinessException("验证码输入错误，请重新输入");
        }
        // 6. 保存数据   user， logininfo
        // 先去获取logininfo对象和user对象
        Logininfo logininfo = dto2Logininfo(dto);
        // 通过logininfo对象获取user对象
        User user = logininfo2User(logininfo);

        // 先保存logininfo表，返回自增的主键
        logininfoMapper.save(logininfo);

        // 把自增的主键赋值给user对象， 再保存user。
        user.setLogininfo_id(logininfo.getId());
        userMapper.save(user);
    }


    private User logininfo2User(Logininfo logininfo) {

        User user = new User();
        // copy相同的属性
        BeanUtils.copyProperties(logininfo,user);

        return  user;
    }

    /**
     * 创建logininfo对象
     * @param dto
     * @return
     */
    public Logininfo dto2Logininfo(RegisterDto dto){
        Logininfo logininfo = new Logininfo();

        logininfo.setUsername(dto.getPhone());
        logininfo.setPhone(dto.getPhone());
        logininfo.setType(1);
        // 生成盐值 32
        String salt = StrUtils.getComplexRandomString(32);
        // 通过和密码进行加密 加盐加密：
        String dbPassword = Md5Utils.encrypByMd5(salt + dto.getPassword());
        logininfo.setSalt(salt);
        logininfo.setPassword(dbPassword);
        return logininfo;
    }
}
